﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace ECGWeb
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        static ECGService.ECGServiceClient serviceClient = new ECGService.ECGServiceClient();
        static ECGService.HistoryRecord[] historyRecordArray = null;
        static ECGService.Patient[] patientArray = null;
        static ECGService.UnusualSignal[] unusualSignalArray = null;
        static ECGService.UnusualSignal unusualSignal = null;
        static ECGService.Patient patient;
        static ECGService.User user = new ECGService.User();
        static string watchingRecordID;
        static int timeCount = 0;
        static ECGService.Comment[] cmtArr = null;
        static bool checknewpatient = false;
        static string recordDir;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
            {
                if (!Page.IsPostBack)
                {
                    Button btn = (Button)Master.FindControl("btn_patientpage");
                    btn.BackColor = System.Drawing.Color.Gray;

                    user.username = (string)Session["username"];
                    user.realID = (string)Session["realID"];
                    user.permission = (int)Session["permission"];

                    this.loadData(user.realID);
                }
                else
                {
                    if (cmtArr != null)
                    {
                        commentDataTable.setDataSource(cmtArr);
                    }
                    else
                    {
                        commentDataTable.fakeCommentGridViewData();
                    }

                    if (checknewpatient)
                    {
                        this.notifyLbl.Text = "";
                        this.fromDateTxtBox.Text = "";
                        this.toDateTxtBox.Text = "";
                        checknewpatient = false;
                    }
                }

                patientShortDataTable.setDataSource(patientArray);

                //unusualSignal
                this.unusualList.setDataSource(unusualSignalArray);
                if (unusualSignal != null)
                {
                    uHR.Text = "HR: " + unusualSignal.hr;
                    uR.Text = "R: " + unusualSignal.r;
                    uQR.Text = "QR: " + unusualSignal.qr;
                    uRS.Text = "RS: " + unusualSignal.rs;
                    uQRS.Text = "QRS: " + unusualSignal.qrs;
                    uInfoUpdatePanel.Update();
                }
                else
                {
                    uHR.Text = "HR: ";
                    uR.Text = "R: ";
                    uQR.Text = "QR: ";
                    uRS.Text = "RS: ";
                    uQRS.Text = "QRS: ";
                    uInfoUpdatePanel.Update();
                }

                if (historyRecordArray != null)
                {
                    this.gridview1.DataSource = historyRecordArray;
                    this.gridview1.DataBind();
                }
                else
                {
                    fakeRecordGridViewData(this.gridview1);
                }

                this.patientInformation.setData(patient);                              
            }
            else
                Response.Redirect("~/HomePage.aspx");
        }

        public void loadData(string docID)
        {
            patientArray = serviceClient.getPatientByResDoctor(docID);
            historyRecordArray = serviceClient.getAllRecordByPID(patientArray[0].patientID);
            patient = serviceClient.getPatientByID(patientArray[0].patientID);
            unusualSignalArray = null;
            unusualSignal = null;
        }               

        [WebMethod]
        public static string loadECGRecord(string recordID)
        {
            watchingRecordID = recordID;
            timeCount = 0;
            return serviceClient.loadECGRecord(recordID);
        }

        [WebMethod]
        public static string loadECGRecord12(string recordID)
        {
            watchingRecordID = recordID;
            timeCount = 0;
            return serviceClient.loadECGRecord12(recordID);
        }

        [WebMethod]
        public static string loadAFrame(string recordID)
        {
            watchingRecordID = recordID;
            unusualSignalArray = serviceClient.getUnusualSignalOfRecord(recordID);
            unusualSignal = null;            
            recordDir = serviceClient.getRecordByID(recordID).recordDir;
            string result = serviceClient.loadAFrameFromRecord(recordDir, 0);            
            return result;
        }

        [WebMethod]
        public static string continueLoadAFrame(long offset)
        {
            string result = serviceClient.loadAFrameFromRecord(recordDir, offset);
            return result;
        }

        [WebMethod]
        public static string LoadUnusualInfo(int id)
        {
            unusualSignal = serviceClient.getUnusualSignal(id);
            return "ok";
        }

        protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridview1.PageIndex = e.NewPageIndex;
            this.gridview1.Page.DataBind();
        }

        protected void fromDateTxtBox_TextChanged(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            if (DateTime.TryParse(this.fromDateTxtBox.Text, out fromDate))
            {
                if (DateTime.TryParse(this.toDateTxtBox.Text, out toDate))
                {
                    if (toDate >= fromDate)
                    {
                        toDate.AddDays(1);
                        //Array.Clear(historyRecordArray, 0, historyRecordArray.Length);
                        historyRecordArray = serviceClient.getRecordByPIDBetween(patient.patientID, fromDate, toDate);

                        if (historyRecordArray != null)
                        {
                            this.gridview1.DataSource = historyRecordArray;
                            this.gridview1.DataBind();
                        }
                        else
                        {
                            fakeRecordGridViewData(this.gridview1);
                        }

                        if (historyRecordArray == null || historyRecordArray.Length == 0)
                            notifyLbl.Text = "Không có bản ghi nào trong khoảng thời gian trên";
                        else
                            notifyLbl.Text = "";
                    }
                    else
                        notifyLbl.Text = "Bạn đang chọn từ ngày nhỏ hơn đến ngày! Vui lòng nhập chính xác!";
                }
            }
        }

        protected void toDateTxtBox_TextChanged(object sender, EventArgs e)
        {
            DateTime fromDate = new DateTime();
            DateTime toDate = new DateTime();

            if (DateTime.TryParse(this.toDateTxtBox.Text, out toDate))
            {
                if (DateTime.TryParse(this.fromDateTxtBox.Text, out fromDate))
                {
                    if (toDate >= fromDate)
                    {
                        toDate.AddDays(1);
                        historyRecordArray = serviceClient.getRecordByPIDBetween(patient.patientID, fromDate, toDate);

                        if (historyRecordArray != null)
                        {
                            this.gridview1.DataSource = historyRecordArray;
                            this.gridview1.DataBind();
                        }
                        else
                        {
                            fakeRecordGridViewData(this.gridview1);
                        }

                        if (historyRecordArray == null || historyRecordArray.Length == 0)
                            notifyLbl.Text = "Không có bản ghi nào trong khoảng thời gian trên";
                        else
                            notifyLbl.Text = "";
                    }
                    else
                        notifyLbl.Text = "Bạn đang chọn từ ngày nhỏ hơn đến ngày! Vui lòng nhập chính xác!";
                }
            }
        }

        public void fakeRecordGridViewData(GridView gridview)
        {
            ECGService.HistoryRecord[] historyRecordArrayFake = new ECGService.HistoryRecord[1];
            historyRecordArrayFake[0] = new ECGService.HistoryRecord();
            historyRecordArrayFake[0].recordID = "fake";
            historyRecordArrayFake[0].dateTimeOfRecord = DateTime.Now;

            gridview.DataSource = historyRecordArrayFake;
            gridview.DataBind();
            gridview.Rows[0].Visible = false;
            gridview.Rows[0].Controls.Clear();
        }

        protected void comment_btn_Click(object sender, EventArgs e)
        {
            cmtArr = serviceClient.getCommentByRID(watchingRecordID);
            if (cmtArr != null)
            {

                commentDataTable.setDataSource(cmtArr);
            }
            else
            {
                commentDataTable.fakeCommentGridViewData();
            }
        }

        [WebMethod]
        public static string GetCommentContent(string docID, string RID)
        {
            return serviceClient.getCommentContent(RID, docID);
        }

        [WebMethod]
        public static string EditComment(string rID, string docID, string content)
        {
            int result = serviceClient.editComment(rID, docID, DateTime.Now, content);
            cmtArr = serviceClient.getCommentByRID(watchingRecordID);
            return result.ToString();
        }

        [WebMethod]
        public static string AddComment(string RID, string DID, string content)
        {
            int result = serviceClient.addNewComment(RID, DID, DateTime.Now, content);
            cmtArr = serviceClient.getCommentByRID(watchingRecordID);
            if (result == 1)
            {
                return DateTime.Now.ToString();
            }
            else
                return result.ToString();
        }

        [WebMethod]
        public static string ShowPatientInfo(string patientID)
        {
            checknewpatient = true;
            watchingRecordID = "";
            historyRecordArray = serviceClient.getAllRecordByPID(patientID);
            patient = serviceClient.getPatientByID(patientID);
            unusualSignalArray = null;
            return "ok";
        }

        protected void rs_ddlOnTextChanged(object sender, EventArgs e)
        {
            if (this.rs_ddl.Text == "Bệnh nhân khác")
            {
                patientArray = serviceClient.getPatientByNotResDoctor(user.realID);
            }
            else
            {
                patientArray = serviceClient.getPatientByResDoctor(user.realID);
            }
            patientShortDataTable.setDataSource(patientArray);
        }

        [WebMethod]
        public static string deleteComment(string RID, string DocID)
        {
            string s = serviceClient.deleteComment(RID, DocID);
            cmtArr = serviceClient.getCommentByRID(watchingRecordID);
            return s;
        }

        [WebMethod]
        public static string searchPatient(String text, String category)
        {
            if (category == "Bệnh nhân khác")
            {
                patientArray = serviceClient.getPatientByName_notResDoctor(text, user.realID);
            }
            else
            {
                patientArray = serviceClient.getPatientByName_resDoctor(text, user.realID);
            }
            return patientArray.Length.ToString();
        }
    }
}